package com.qms.qep.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qms.qep.mapper.DeviceCalibrationMapper;
import com.qms.qep.mapper.EquipmentMapper;
import com.qms.qep.domain.DeviceCalibration;
import com.qms.qep.domain.Equipment;
import com.qms.qep.service.IDeviceCalibrationService;

/**
 * 设备校准记录Service业务层处理
 * 
 * @author qep
 */
@Service
public class DeviceCalibrationServiceImpl implements IDeviceCalibrationService 
{
    @Autowired
    private DeviceCalibrationMapper deviceCalibrationMapper;
    
    @Autowired
    private EquipmentMapper equipmentMapper;

    /**
     * 查询设备校准记录
     * 
     * @param id 设备校准记录主键
     * @return 设备校准记录
     */
    @Override
    public DeviceCalibration selectDeviceCalibrationById(Long id)
    {
        DeviceCalibration deviceCalibration = deviceCalibrationMapper.selectDeviceCalibrationById(id);
        if (deviceCalibration != null && deviceCalibration.getEquipmentId() != null) {
            Equipment equipment = equipmentMapper.selectEquipmentById(deviceCalibration.getEquipmentId());
            if (equipment != null) {
                deviceCalibration.setEquipmentName(equipment.getName());
            }
        }
        return deviceCalibration;
    }

    /**
     * 查询设备校准记录列表
     * 
     * @param deviceCalibration 设备校准记录
     * @return 设备校准记录
     */
    @Override
    public List<DeviceCalibration> selectDeviceCalibrationList(DeviceCalibration deviceCalibration)
    {
        List<DeviceCalibration> list = deviceCalibrationMapper.selectDeviceCalibrationList(deviceCalibration);
        for (DeviceCalibration calibration : list) {
            if (calibration.getEquipmentId() != null) {
                Equipment equipment = equipmentMapper.selectEquipmentById(calibration.getEquipmentId());
                if (equipment != null) {
                    calibration.setEquipmentName(equipment.getName());
                }
            }
        }
        return list;
    }

    /**
     * 新增设备校准记录
     * 
     * @param deviceCalibration 设备校准记录
     * @return 结果
     */
    @Override
    public int insertDeviceCalibration(DeviceCalibration deviceCalibration)
    {
        int rows = deviceCalibrationMapper.insertDeviceCalibration(deviceCalibration);
        // 更新设备的校准信息
        if (rows > 0 && deviceCalibration.getEquipmentId() != null) {
            Equipment equipment = equipmentMapper.selectEquipmentById(deviceCalibration.getEquipmentId());
            if (equipment != null) {
                equipment.setLastCalibration(deviceCalibration.getCalibrationDate());
                equipment.setNextCalibration(deviceCalibration.getNextCalibration());
                equipment.setCalibrationStatus(deviceCalibration.getCalibrationResult());
                equipmentMapper.updateEquipment(equipment);
            }
        }
        return rows;
    }

    /**
     * 修改设备校准记录
     * 
     * @param deviceCalibration 设备校准记录
     * @return 结果
     */
    @Override
    public int updateDeviceCalibration(DeviceCalibration deviceCalibration)
    {
        int rows = deviceCalibrationMapper.updateDeviceCalibration(deviceCalibration);
        // 更新设备的校准信息
        if (rows > 0 && deviceCalibration.getEquipmentId() != null) {
            Equipment equipment = equipmentMapper.selectEquipmentById(deviceCalibration.getEquipmentId());
            if (equipment != null) {
                equipment.setLastCalibration(deviceCalibration.getCalibrationDate());
                equipment.setNextCalibration(deviceCalibration.getNextCalibration());
                equipment.setCalibrationStatus(deviceCalibration.getCalibrationResult());
                equipmentMapper.updateEquipment(equipment);
            }
        }
        return rows;
    }

    /**
     * 批量删除设备校准记录
     * 
     * @param ids 需要删除的设备校准记录主键
     * @return 结果
     */
    @Override
    public int deleteDeviceCalibrationByIds(Long[] ids)
    {
        return deviceCalibrationMapper.deleteDeviceCalibrationByIds(ids);
    }

    /**
     * 删除设备校准记录信息
     * 
     * @param id 设备校准记录主键
     * @return 结果
     */
    @Override
    public int deleteDeviceCalibrationById(Long id)
    {
        return deviceCalibrationMapper.deleteDeviceCalibrationById(id);
    }
} 